
//
//  LinkedList_remove.swift
//  SwiftyDSA
//
//  Created by Archer on 2018/8/6.
//  Copyright © 2018年 Archer. All rights reserved.
//

import Foundation

extension LinkedList {
    public func removeAll() {
        _size = 0
        // 将头节点和尾节点相连
        // 其他节点因为没有引用会被ARC机制释放
        _header.successor = _trailer
        _trailer.predecessor = _header
    }
    
    @discardableResult
    public func remove(_ node: LinkedListNode<Element>) -> Element {
        node.predecessor?.successor = node.successor
        node.successor?.predecessor = node.predecessor
        _size -= 1
        return (node.value)! // it's safe here to force-unwrap
    }
}
